class Restaurants::RestaurantsController < ApplicationController

    layout 'restaurant'
    before_action :check_restaurant_login
    before_action :get_current_restaurant   
    before_action :get_total_info
    before_action :init_breadcrumb

    def check_restaurant_login
      if session[:restaurant_id].blank?
        redirect_to new_restaurants_session_path and return
      end    
    end    

    def get_current_restaurant
      @restaurant = Restaurant.find session[:restaurant_id]
    end

    def get_total_info
      @food_categories_count = @restaurant.food_types.count
      @food_total_count = Food.where(food_type: @restaurant.food_types).count
      @printer_count = @restaurant.printers.count
      @wait_orders_count = @restaurant.orders.where(status: Order::WAIT,is_pay: false).count
      @success_order_count = @restaurant.orders.where(status: Order::SUCCESS).count
      @unread_message_count = @restaurant.restaurant_messages.where(is_read: false).count
    end  

    def welcome
      @nav_title = "首页"
      @today_orders_count = Order.where("to_days(created_at) = to_days(now())").where(restaurant_id: session[:restaurant_id]).count
      new_users = User.find_by_sql [
        "select count(*) as today_count from users where users.id in 
        (
          select orders.user_id from orders where to_days(created_at) = to_days(now()) and restaurant_id = :restaurant_id and status = 3
        ) 
        and users.id not in 
        (
          select user_id from orders where to_days(created_at) != to_days(now()) and restaurant_id = :restaurant_id and status = 3
        )
        ",{restaurant_id: 1}
        ]
        @today_user_count = new_users.first.today_count
    end  

    def new_password
      @nav_title = "修改密码"
      add_breadcrumb create_nav(@nav_title,'#')
    end  

    def update_password
       @nav_title = "修改密码"
      add_breadcrumb create_nav(@nav_title,'#')
      old_password = params[:old_password]
      new_password = params[:new_password]
      password_confirm = params[:password_confirm]
      res = Restaurant.authentication(@restaurant.login,old_password)
      flash[:notice] = nil
      if res.blank?
        flash[:notice] = "原密码错误请重试"
      elsif new_password.blank? 
        flash[:notice] = "新密码不能为空"
      elsif new_password != password_confirm
        flash[:notice] = "2此密码不一致"  
      elsif new_password.length < 6
        flash[:notice]   = "密码不能小于6位"
      end  
      if flash[:notice].present?
        render :new_password and return
      end 
      @restaurant.password = params[:new_password]
      @restaurant.save
      flash[:success] = "密码修改成功"
      redirect_to action: :new_password
    end  

    def init_breadcrumb
        nav = create_nav '首页','/restaurants/welcome'
        @navs = [nav]
    end 

    def add_breadcrumb nav
        @navs.push(nav)
    end 

    def create_nav label,url
        nav = {}
        nav[:label] = label
        nav[:url] = url
        nav
    end   

    private
    def restaurant_params
      params.require(:restaurant).permit(:name,:description,:attachment,:password,:phone,:address,:total_tables,:bulletion)
    end  
end
